/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: GoldenSocialConfig
 * Author:   zhouheng
 * Date:     2019/6/15 8:36
 * Description: 社交配置
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.zhouh.golden.security.config;

import com.zhouh.golden.security.handler.GoldenAuthenticationSucessHandler;
import com.zhouh.golden.security.properties.GoldenSecurityProperties;
import com.zhouh.golden.security.social.GoldenSpringSocialConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfiguration;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.security.SpringSocialConfigurer;

import javax.sql.DataSource;

/**
 * 〈一句话功能简述〉<br> 
 * 〈社交配置〉
 *
 * @author zhouheng
 * @create 2019/6/15
 * @since 1.0.0
 */
@EnableSocial
@Configuration
@Order(1)
public class GoldenSocialConfig extends SocialConfigurerAdapter{
    @Autowired
    DataSource dataSource;

    @Autowired
    private GoldenAuthenticationSucessHandler goldenAuthenticationSucessHandler;

    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
        JdbcUsersConnectionRepository jdbcUsersConnectionRepository =  new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
        jdbcUsersConnectionRepository.setTablePrefix("t_");

        return jdbcUsersConnectionRepository;
    }

    @Bean
    public SpringSocialConfigurer febsSocialSecurityConfig(GoldenSecurityProperties securityProperties) {
        GoldenSpringSocialConfigurer goldenSpringSocialConfigurer = new GoldenSpringSocialConfigurer(securityProperties.getSocial().getFilterProcessesUrl());
        goldenSpringSocialConfigurer.setGoldenSecurityProperties(securityProperties);
        goldenSpringSocialConfigurer.setGoldenAuthenticationSucessHandler(goldenAuthenticationSucessHandler);

        return  goldenSpringSocialConfigurer;
    }

}